package com.yxhl.zoume.core.tripsmgmt.presenter.unit;

import android.content.Context;
import android.text.TextUtils;

import com.yxhl.zoume.common.presenter.BasePresenter;
import com.yxhl.zoume.common.ui.view.BaseView;
import com.yxhl.zoume.core.tripsmgmt.presenter.ipresenter.IRefundTicketPresenter;
import com.yxhl.zoume.core.tripsmgmt.view.unit.RefundTicketView;
import com.yxhl.zoume.data.http.model.trips.TripPassenger;
import com.yxhl.zoume.data.http.rest.param.tripsmgnt.RefundTripParam;
import com.yxhl.zoume.data.http.rest.response.tirps.RefundResponse;
import com.yxhl.zoume.domain.interactor.ZMSubscriber;
import com.yxhl.zoume.domain.interactor.tripsmgmt.TripRefundUseCase;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * Created by sugarya on 16/6/3.
 */
public class RefundTicketPresenter extends BasePresenter implements IRefundTicketPresenter {

    private static final String TAG = "RefundTicketPresenter";

    @Inject
    TripRefundUseCase mTripRefundUseCase;

    private RefundTicketView mRefundTicketView;

    @Inject
    public RefundTicketPresenter(Context context) {
        super(context);
    }

    @Override
    public void attachView(BaseView view) {
        if (view != null) {
            mRefundTicketView = (RefundTicketView) view;
        }
    }

    @Override
    public void refundTicket(String orderId, TripPassenger tripPassenger) {
        if (tripPassenger != null && !TextUtils.isEmpty(orderId)) {
            List<TripPassenger> tripPassengerList = new ArrayList<>();
            tripPassengerList.add(tripPassenger);
            RefundTripParam refundTripParam = new RefundTripParam(
                    getMobile(),
                    getToken(),
                    orderId,
                    tripPassengerList);

            mTripRefundUseCase.execute(refundTripParam, new ZMSubscriber<RefundResponse>() {
                @Override
                public void onCompleted() {
                    super.onCompleted();
                }

                @Override
                public void onError(Throwable e) {
                    super.onError(e);
                }

                @Override
                public void onNext(RefundResponse refundResponse) {
                    super.onNext(refundResponse);
                    if (refundResponse != null) {
                        if (refundResponse.isSucc()) {
                            //关闭退票对话框,更新乘客信息列表的Adapter
                            mRefundTicketView.renderRefundOnSuccess();
                        } else {
                            String resultMessage = refundResponse.getResultMessage();
                            mRefundTicketView.renderRefundOnFailure(resultMessage);
                        }
                    }
                }
            });
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mTripRefundUseCase.unsubscribe();
    }
}
